package com.woniu.userservice.service.impl;

import com.woniu.userservice.entity.Positions;
import com.woniu.userservice.dao.PositionsDao;
import com.woniu.userservice.service.PositionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("positionsService")
public class PositionsServiceImpl implements PositionsService {
    @Resource
    private PositionsDao positionsDao;

    @Override
    public Positions queryById(Integer id) {
        return positionsDao.queryById(id);
    }

    @Override
    public List<Positions> queryAllByLimit(int offset, int limit) {
        return positionsDao.queryAllByLimit(offset, limit);
    }
   
     @Override
    public List<Positions> findAll() {
        return positionsDao.findAll();
    }

    @Override
    public Positions insert(Positions positions) {
        positionsDao.insert(positions);
        return positions;
    }

    @Override
    public Positions update(Positions positions) {
        positionsDao.update(positions);
        return queryById(positions.getId());
    }

    @Override
    public boolean deleteById(Integer id) {
        return positionsDao.deleteById(id) > 0;
    }

    /**
     *
     * @param userid
     * @return 查询该用户是否存在默认地址
     */
    @Override
    public  List<Positions> findById(Integer userid) {
        return positionsDao.findById(userid);
    }

    /**
     *
     * @param positioninfo
     * @param receivername
     * @param phonenumber
     * @param postnumber
     * @param isdefault
     * @param userid
     * 插入地址
     */
    @Override
    public void insertMsg(String positioninfo, String receivername, String phonenumber, Integer postnumber, Integer isdefault, Integer userid) {
        positionsDao.insertAll(userid,phonenumber,receivername,postnumber,positioninfo,isdefault);
    }

    /**
     *
     * @param positioninfo
     * @param receivername
     * @param phonenumber
     * @param postnumber
     * @param isdefault
     * @param userid
     * 插入地址
     */
    @Override
    public void insertMsgTwo(String positioninfo, String receivername, String phonenumber, Integer postnumber, Integer isdefault, Integer userid) {
        positionsDao.insertAll(userid,phonenumber,receivername,postnumber,positioninfo,isdefault);
    }

    /**
     * 这个是将原来的默认地址改为不是默认地址
     */
    @Override
    public void updateByIsdefault() {
        positionsDao.updateByIsdefault();
    }

    /**
     *
     * @param uid
     * @return 该用户所有地址
     */
    @Override
    public List<Positions> findByUid(Integer uid) {
        if (uid==null){
            return null;
        }else{
        return positionsDao.findByUid(uid);
        }
    }

    /**
     *
     * @param uid
     * @return 用户的默认地址
     */
    @Override
    public List<Positions> findByOneUid(Integer uid) {
        return positionsDao.findByOneUid(uid);
    }

    /**
     *
     * @param id
     * @param userid
     * @return 删除用户地址
     */
    @Override
    public Boolean deleteByuserId(Integer id, Integer userid) {
        if (id == null && userid ==null){
            return false;
        }
        return positionsDao.deleteByUserId(id,userid);
    }

    /**
     * 更改原来的默认地址为不是默认地址
     */
    @Override
    public void updateByIsdefaultOne(Integer userid) {
        positionsDao.updateByIsdefaultOne(userid);
    }

    @Override
    public void insertMsgThree(String positioninfo, String receivername, String phonenumber, Integer postnumber, Integer isdefault, Integer userid) {
        /**
         * 先查询 是否存在默认地址
         *  如果不存在 直接插入
         *      如果存在 则先取消原来的默认地址
         */
        List<Positions> positions = positionsDao.findById(userid);
        if (positions.size()>0){
            Boolean bo = positionsDao.updateByIsdefaultOne(userid);
            if (bo){
                positionsDao.insertAll(userid, phonenumber,  receivername,  postnumber,  positioninfo, isdefault);
            }
        }else {
            positionsDao.insertAll(userid, phonenumber,  receivername,  postnumber,  positioninfo, isdefault);
        }

    }


}